$(function(){
	var openWindow=window.document;
	var searchHeight=$("#divform").height();
	$("#listDepart").height($(openWindow).height()-$(openWindow).height()*0.05-searchHeight-10);
	$("#listDepart").datagrid({
					url:'/base/getDepartment',
					title:'部门列表',
					method:'get',
					pagination:true,
					height:400,
					columns:[[
					         {field:'ck',checkbox:true},
					         {field:'departmentcode',title:'部门编号', width:100},
					         {field:'departmentname',title:'部门名称',width:100},
					         {field:'isflag',title:'状态',width:60,formatter: function(value){
					        	 if(value){
					        		 return '已停用';
					        	 }
					        	 else{
					        		 return '已启用';
					        	 }
					         }},
					         {field:'sort',title:'排序',width:60},
					         {field:'departmentid' ,align:'center',title:'操作',width:120,formatter: function(value, row, index){
					        	return '<a href="javascript:void(0); class="a_control">详情</a>&nbsp;'+
					        	'<a href="javascript:void(0);class="a_control">编辑</a>&nbsp;'+
					        	'<a href="javascript:void(0);class="a_control">删除</a>';
					         }}
					     ]]
				});
	$("#listDepart").datagrid({
		toolbar: '#tb'
	});
	$("#serbutton").click(function(){
		var departmentCode=$("#departmentCode").val();
		var departmentName=$("#departmentName").val();
		var params={};
		params.departmentCode=departmentCode;
		params.departmentName=departmentName;
		$("#listDepart").datagrid('load',params);
	});
});
/**
 * 启用部门
 */
function enabledepart(){
	var rows=$("#listDepart").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.alert('启用','确定启用该部门吗？','question',function(){
		var ids='';
		for(var i=0;i<rows.length;i++){
			ids+=rows[i].departmentid+',';
		}
		$.ajax({
			type:'post',
			url:'/base/enableDepartment',
			data:{id:ids},
			success:function(data){
				if(data.code=='999'){
					$.messager.alert('失败','启用失败:'+data.message,'error');
				}
				if(data.code=='200'){
					$("#listDepart").datagrid('reload');
				}
			},
		})
	});
}
/**
 * 停用部门
 */
function disabledepart(){
	var rows=$("#listDepart").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.alert('停用','确定停用该部门吗？','question',function(){
		var ids='';
		for(var i=0;i<rows.length;i++){
			ids+=rows[i].departmentid+',';
		}
		$.ajax({
			type:'post',
			url:'/base/disableDepartment',
			data:{id:ids},
			success:function(data){
				if(data.code=='999'){
					$.messager.alert('失败','停用失败:'+data.message,'error');
				}
				if(data.code=='200'){
					$("#listDepart").datagrid('reload');
				}
			},
		})
	});
}
/**
 * 批量删除部门
 */
function betchdeldepart(){
	var rows=$("#listDepart").datagrid('getSelections');
	if(rows.length<=0){
		$.messager.alert('错误','选中的记录为空','error');
		return;
	}
	$.messager.alert('删除','确定删除该部门吗？','question',function(){
		var ids='';
		for(var i=0;i<rows.length;i++){
			ids+=rows[i].departmentid+',';
		}
		$.ajax({
			type:'post',
			url:'/base/betchDeleteDepartment',
			data:{id:ids},
			success:function(data){
				if(data.code=='999'){
					$.messager.alert('删除','删除失败:'+data.message,'error');
				}
				if(data.code=='200'){
					$("#listDepart").datagrid('reload');
				}
			},
		})
	});
}
/**
 * 添加页面
 */
function adddepart(){
	//window.parent.addShade();
	window.parent.openDialog('添加部门','/base/addDepartment','',400,230,'id=1');
}
